package com.comparable_comparator.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
/**
 * @author Deepak
 */
public class Animal implements Comparable,Comparator{
	public Animal(){}
	/**
	 *
	 */
int animal_id;
String animal_name;
public Animal(int animal_id,String animal_name){
	this.animal_id=animal_id;
	this.animal_name=animal_name;
}
/**
 * 
 * @param param1
 * @throws no exceptions
 */
public static void main(String[] args) {
	System.out.println("For Comparable Interface Sorting");
	ArrayList<Animal> al=new ArrayList<Animal>();
	al.add(new Animal(102, "Cow"));
	al.add(new Animal(101, "Cat"));
	al.add(new Animal(104, "Lion"));
	al.add(new Animal(103, "Tiger"));
	Collections.sort(al);
	
	/*Iterator<Animal> itr=al.iterator();
	while(itr.hasNext()){
		Animal ani=(Animal)itr.next();
		System.out.println(ani.animal_id+"\t"+ani.animal_name);
	}*/
	//System.out.println("For Comparator Interface Sorting");
	//Collections.sort(al, new Animal());
	/*Iterator<Animal> itr2=al.iterator();
	while(itr2.hasNext()){
		Animal ani=(Animal)itr2.next();
		System.out.println(ani.animal_id+"\t"+ani.animal_name);
	}*/
	for(Animal an:al){
		System.out.println(an.animal_id+"\t"+an.animal_name);
	}
}
@Override
public int compareTo(Object o) {
	Animal a=(Animal)o;
	//Integer i=this.animal_id;
	//return i.compareTo(a.animal_id);
	return this.animal_id-a.animal_id;
}
@Override
public int compare(Object o1, Object o2) {
	Animal a1=(Animal)o1;
	Animal a2=(Animal)o2;
	String name1=a1.animal_name;
	String name2=a2.animal_name;
	return name1.compareTo(name2);
}
}
